package com.xs.shop.orders.service.controller;

import com.xs.shop.common.PageResult;
import com.xs.shop.common.Result;
import com.xs.shop.goods.model.entity.Goods;
import com.xs.shop.orders.model.entity.Cart;
import com.xs.shop.orders.model.entity.Order;
import com.xs.shop.orders.service.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xs,zyx
 * @since 2025-07-22
 */
@Slf4j
@RestController
@RequestMapping("/order")
@Tag(name = "订单管理", description = "订单的CRUD操作")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Operation(summary = "新增订单", description = "创建一个新的订单")
    @ApiResponse(responseCode = "200", description = "创建成功")
    @PostMapping
    public Result<Object> addOrder(@RequestBody Order order) {
        log.info("创建订单：{}", order);
        orderService.addOrder();
        return Result.success();
    }
//    /**
//     * 添加商品
//     * @param goods 商品信息
//     * @return 添加结果
//     */
//    @Operation(summary = "添加商品", description = "添加新的商品信息")
//    @ApiResponse(responseCode = "200", description = "添加成功")
//    @PostMapping("")
//    public Result<Object> add(@RequestBody Goods goods) {
//        log.info("添加商品：{}", goods);
//        goodsService.save(goods);
//        return Result.success();
//    }
    @DeleteMapping("/{id}")
    @Operation(summary = "删除订单", description = "根据ID删除订单")
    public Result<Boolean> deleteOrder(@PathVariable @Parameter(description = "订单ID") Long id) {
        boolean result = orderService.deleteOrder(id);
        return Result.success(result);
    }

    @PutMapping
    @Operation(summary = "更新订单", description = "更新订单信息")
    public Result<Order> updateOrder(@RequestBody Order order) {
        Order updatedOrder = orderService.updateOrder(order);
        return Result.success(updatedOrder);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取订单详情", description = "根据ID获取订单详情")
    public Result<Order> getOrderById(@PathVariable @Parameter(description = "订单ID") Long id) {
        Order order = orderService.getOrderById(id);
        return Result.success(order);
    }
    @GetMapping("getOrder")
    @Operation(summary = "获取会员订单", description = "根据会员ID获取订单列表")
    public Result<List<Order>> getOrdersByLoginId() {
        List<Order> ordersList = orderService.getOrdersByLoginId();
        return Result.success(ordersList);
    }
    @DeleteMapping("")
    @Operation(summary = "清空订单（仅测试）", description = "根据MemberID清空订单")
    public Result<Boolean> deleteCart() {
        boolean result = orderService.clearOrder();
        return Result.success(result);
    }
    @GetMapping("/page")
    @Operation(summary = "分页查询订单", description = "分页查询订单列表，支持按订单ID、订单号、会员ID和状态查询")
    public PageResult<Order> pageOrder(
            @RequestParam(defaultValue = "1") @Parameter(description = "当前页码") Integer current,
            @RequestParam(defaultValue = "10") @Parameter(description = "每页记录数") Integer pageSize,
            @RequestParam(required = false) @Parameter(description = "订单ID") Long orderId,
            @RequestParam(required = false) @Parameter(description = "订单编号") String orderNo,
            @RequestParam(required = false) @Parameter(description = "会员ID") Long memberId,
            @RequestParam(required = false) @Parameter(description = "订单状态(0-待付款,1-待发货,2-待收货,3-已完成,4-已取消)") Integer status
    ) {
        return PageResult.success(orderService.pageOrder(current, pageSize, orderId, orderNo, memberId, status));
    }
}
